package com.jinyou.chengfengshop.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.MainActivity;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;
import com.jinyou.chengfengshop.activity.OrderDetailsActivity;
import com.jinyou.chengfengshop.bean.orderNoBean;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * 首页订单
 * Created by zhuzhiqiang on 2018/9/27.
 */

public class HomeOrderAdapter extends RecyclerView.Adapter<HomeOrderAdapter.ViewHolder> {
    private List<orderNoBean> orb;
    private Context mContext;
    private int itemX;

    public HomeOrderAdapter(int itemX,List<orderNoBean> orb, Context mContext) {
        this.orb = orb;
        this.mContext = mContext;
        this.itemX = itemX;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.scroll_right, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        String time = formatData("yyyy-MM-dd HH:mm:ss", orb.get(position).getCreateTim());
        holder.time.setText(time.substring(5,time.length()));
        Glide.with(mContext).load(orb.get(position).getShopImage()).thumbnail( 0.2f ).into(holder.img);
//        Picasso.with(mContext)
//                .load(orb.get(position).getShopImage())
//                .into(holder.img);
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(itemX == 4){
                    final String[] strArray ={"确认","取消"};
                    AlertDialog.Builder builder2 = new AlertDialog.Builder(mContext);//实例化builder
                    builder2.setIcon(R.mipmap.ic_launcher);//设置图标
                    builder2.setTitle("是否要取消配送该订单？");//设置标题
                    //设置列表
                    builder2.setItems(strArray, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which){
                                case 0:
                                        cancelOrder(orb.get(position).getOrderNo(),position);

                                    break;

                                case 1:

                                    break;
                            }
                        }
                    });

                    builder2.create().show();//创建并显示对话框
                    return;
                }

                Intent mIntent = new Intent(MainActivity.context, OrderDetailsActivity.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mIntent.putExtra("ORDER",orb.get(position));
                MainActivity.context.startActivity(mIntent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return orb.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView time;
        private ImageView img;
        public ViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.right_text);
            img = (ImageView) itemView.findViewById(R.id.img_order);


        }
    }
    private String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }

        String mm = timeStamp+"";
        mm = mm.substring(0,10);
        timeStamp = Long.parseLong(mm);

        timeStamp = timeStamp * 1000;
        String result = "";
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(timeStamp));
        return result;
    }


    //取消发单
    private void cancelOrder(String orderNo,final int postion){
        HashMap<String,String> paramsMap = new HashMap<>();
        SharedPreferences sh = mContext.getSharedPreferences("user", Context.MODE_PRIVATE);
        final String tocken = sh.getString("tocken","");
        paramsMap.put("token",tocken);
        paramsMap.put("orderNo",orderNo);
        paramsMap.put("sysCustomer","o2o-chengfeng");
        OkhttpUtil.okHttpPost(AppUrl.SHOP_ORDER_CANCEL, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {
                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");

                    if(code == 1){
                        orb.remove(postion);
                        notifyItemRemoved(postion);
                        notifyDataSetChanged();
                    }else {

                        Toast.makeText(mContext,obj.getString("error"),Toast.LENGTH_SHORT).show();
                        if(obj.getString("type").equals("notLogin")){
                            //reLogin();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

}
